//初始化数据
initTies();
var ties = JSON.parse(localStorage.getItem('ties'));
var is_Login = false;
var uid = "";
var user_now_city = "";

initUsers();
var users = getUsers();
var user;

//初始化登入数据
if(AutoLogin()){
	console.log("自动登入成功!");
	is_Login = true;
	uid = $.cookie('uid');
	$("#login_icon").fadeOut();
	$("#register_icon").fadeOut();
	$("#comment_text").attr('placeholder', '发表的评论内容~');
	user = AutoLogin();
	$('.username').text(user.username);
	has_login = true;
}else{
	$("#quit_icon").fadeOut();
	$('.username').text("未登入");
	$("#comment_body_inner *").attr('disabled','true');
	$("#comment_text").attr('placeholder','请先登入后再抒发评论哦~');
}


getWeatherAndLocation();

//初始化填充主体内容面板.
resolveData();
function resolveData() {
	
	//检查url参数是否合法
	if(getUrlValue('page') == undefined){
		setTiePage(1);
	}

	if(getUrlValue('pid') == undefined){
		alert('非法访问URL,该页面不存在');
	}
	
	ties = JSON.parse(localStorage.getItem("ties"));
	var is_exist_pid = false;
	var logs;
	//楼主层的数据来源于帖子pid
	var insert_html="";
	$.each(ties,function(k,v){
		if(v.id == getUrlValue('pid')){
			logs=v['log'];
			if(v.uid == '系统推送'){
					insert_html = `
						<div id="firstFloor">
							<div id="firstFloor_Title">${v.title}
								<div id="firstFloor_Hot">
									热度: ${v.hot}
									<svg class="CustomIcon">
									<use xlink:href="#icon-shengdanwazi"></use>
									</svg>
								</div>
							</div>
							<div id="firstFloor_Info">
								<div id="firstFloor_Left">
									<img id="firstFloor_Img" src="imgs/logo.png" alt="" />
									<div id="firstFloor_User">系统推送</div>
								</div>
								<div id="firstFloor_Text">
								${v.describe}
								<img src="${v.img}" alt="" style='margin:10px 0px 10px 0px;width:100%;height:180px'/>
								<div id="firstFloor_Time">IP归属地: ${v.location} 时间: 刚刚 </div>
								</div>
							</div>
						</div>
					`;

				
			}else{
				
				
			var who_publish = null;
			if(users.find(function(user) { return user.uid === v.uid;})){
				who_publish = users.find(function(user) { return user.uid === v.uid;});
			}
				insert_html = `
					<div id="firstFloor">
						<div id="firstFloor_Title">${v.title}
							<div id="firstFloor_Hot">
								热度: ${v.hot}
								<svg class="CustomIcon">
								<use xlink:href="#icon-shengdanwazi"></use>
								</svg>
							</div>
						</div>
						<div id="firstFloor_Info">
							<div id="firstFloor_Left">
								<img id="firstFloor_Img" src="${who_publish.head_img}" alt="" />
								<div id="firstFloor_User">${who_publish.username}</div>
							</div>
							<div id="firstFloor_Text">
							${v.describe}
							<div id="firstFloor_Time">IP归属地: ${v.location} 时间: ${TimeToStr(v.time)} </div>
							</div>
						</div>
					</div>
				`;
				
			}
			

			return false;
		}
	})
	

	//填充回复楼层
	var add_num = 0;
	now_page_logs = logs.slice(5*(getUrlValue('page') - 1),(getUrlValue('page') )*5 );
	$.each(now_page_logs,function(k,v){

		if(add_num > 4){
			return false;
		}

		var who_msg = null;
		if(users.find(function(user) { return user.uid === v.uid;})){
			who_msg = users.find(function(user) { return user.uid === v.uid;});
		}
		
		
		
		insert_html+=`
		
		<div id="firstFloor">
			<div id="firstFloor_Info">
				<div id="firstFloor_Left">
					<img id="firstFloor_Img" src="${who_msg.head_img}" alt="" />
					<div id="firstFloor_User">${who_msg.username}</div>
				</div>
				
				<div id="firstFloor_Text">
				${v.msg}
				<div id="firstFloor_Time">IP归属地: ${v.location} 时间: ${TimeToStr(v.time)} </div>
				</div>
				
			</div>
		</div>
		
		
		`;
		
		add_num++;
	})
	
	
	

	var page_num = getUrlValue("page");
	//初始化页码active
	$("#page_change li").find("active").removeClass("active");
	if ($("#page_change li").eq(page_num - 1) !== null) {
		$("#page_change li").eq(page_num - 1).addClass('active');
	}




	$("#content_inner_container").html(insert_html);
}

//当前页面增加点击热度
addHot();

$("#comment_button").click(function() {
	$new_obj = {
		'uid': user.uid,
		'msg': $("#comment_text").val(),
		'time': Date.now(),
		'location': user_now_city
	};
	ties = JSON.parse(localStorage.getItem('ties'));
	ties.find(function(tie){return tie.id == getUrlValue('pid')})['log'].push($new_obj);
	localStorage.setItem("ties", JSON.stringify(ties));
	alert("发表评论成功!");
	setTiePage('1');
});

$("#page_change li").click(function() {
	setTiePage($(this).text());
});
$("#page_next").click(function() {
	setTiePage(parseInt(getUrlValue('page')) + 1);
})
$("#page_prev").click(function() {
	setTiePage(parseInt(getUrlValue('page')) - 1);
})
$("#page_end").click(function() {
	setTiePage(Math.floor(ties.length / 5) + 1);
})
$("#page_first").click(function() {
	setTiePage("1");
})


$("#ChangeTheme").click(function() {

	if ($(this).css('background-color') == 'rgb(255, 255, 255)') {
		$(this).css({
			'background-color': 'black'
		});
		location.reload(true);
	} else {

		$(this).css({
			'background-color': 'white'
		});
		$("#top_ul").css({
			'background-color': 'black'
		}).find("a").css({
			'color': 'white'
		});
		$("#main_body").css({
			'background-color': 'black'
		});
		$("#search_container").css({
			'background-color': 'black'
		}).find("input").css({
			'background-color': 'black'
		}).parent().find("button").css('background-color', 'black');
		$("#top_line").css({
			'background-color': 'black'
		});
		$(".tabNavs").css({
			'background-color': 'black'
		});
		$("#slide_body").css({
			'background-color': 'black'
		});
		$("#comment_body_inner").css({
			'background-color': 'black'
		});
		$("#comment_text").css({
			'background-color': 'black'
		});
		$("#comment_input").css({
			'background-color': 'black'
		});
		$("#comment_button").css({
			'background-color': 'black'
		});
		$("#page_change").css({
			'background-color': 'black'
		});
		$("button").css({
			'background-color': 'black'
		});
		$(".tabCon_Every").css({
			'background-color': 'black'
		});
		$("footer").css({
			'background-color': 'black'
		});
		$('.slideTopic').css({
			'background-color': 'black'
		});
		$('.bannertext').css({
			'background-color': 'black'
		});
		$('#search_result_container').css({
			'background-color': 'grey'
		});
		$('#page_change ul').css({
			'background-color': 'black'
		})
		$('#placeholder_window').css({
			'background-color': 'black'
		})
		$.each($('*'), function() {

			if ($(this).css('border').indexOf('none') < 0) {
				$(this).css({
					'border': '1px solid rgb(60,60,60)'
				});
			}
		})
		$('body').css({
			'cursor': 'url("imgs/cur_2.png"),auto'
		});
		$('a').css('color', 'white');
		$('*').css('color', 'white');

	}
})



//禁用原先的a事件 防止干扰跳转url
$(".tabNavs li a").css('pointer-events', 'none');
$(".tabNavs li").click(function() {
	var mode = $(this).text().trim();
	if (mode == '最新') {
		window.open('index.html?page=1&mode=new');
	} else if (mode == '最热') {
		window.open('index.html?page=1&mode=hot');		
	} else {
		window.open('index.html?page=1&mode=recommend');
	}

});

var talks = "";
$.each(getHotestData().slice(0, 6), function(k, v) {
	talks += `<li class="talks_box_con"><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></li>`
});
$("#talks_box").html(talks);


var publics = "";
$.each(getLatestData().slice(0, 6), function(k, v) {
	publics += `<li class="talks_box_con"><a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a></li>`
});
$("#public_box").html(publics);



$("#search_container_input input").on('focus', function() {
	$("#search_result_container").fadeIn();
}).on('blur', function() {
	$("#search_result_container").fadeOut();
}).on('input', function() {
	var text = $(this).val();
	if (text.trim() == "") {
		$("#search_result_container_navs").html("");
		return;
	}
	var results = "";
	$.each(ties, function(k, v) {
		random_icon = random_css_icons[Math.floor(Math.random() * random_css_icons.length)];
		if (v.title.indexOf(text) > -1 || v.describe.indexOf(text) > -1 || v.uid.indexOf(text) > -1) {
			results += `
			<li id="search_result_container_con">
				<svg class="CustomIcon">
				<use xlink:href="#icon-${random_icon}"></use>
				</svg>
				<a target="_blank" href="tie.html?pid=${v.id}">${v.title}</a>
			</li>
			`;
		}
	})
	$("#search_result_container_navs").html(results);

})

$("#search_button").click(function(){
	window.open($("#search_result_container_navs li").eq(0).find('a').attr('href'));
})

